package com.publicize.commom.commonutils;


import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

//自定义拦截器
@Component
    public class TokenInterceptor implements HandlerInterceptor {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            JSONObject jsonObject=new JSONObject();
            PrintWriter out = null;
            String token = request.getHeader("token");
            if(JwtUtils.checkToken(token)||token!=null){
                return true;
            }
            out=response.getWriter();
            jsonObject.put("code",201);
            jsonObject.put("message","token time out");
            out.write(jsonObject.toString());
            out.flush();
            out.close();
            return false;

        }
    }

